class Vote < ActiveRecord::Base

  hobo_model

  belongs_to :election, :counter_cache => true
  belongs_to :candidate, :counter_cache => true

  # --- Validation --- #
  
  validates_presence_of :election_id, :uuid
  
  # --- Hobo Permissions --- #

  def creatable_by?(creator)
    false
  end

  def updatable_by?(updater, new)
    false
  end

  def deletable_by?(deleter)
    false
  end

  def viewable_by?(viewer, field)
    false
  end
  
  # --- Methods --- #
  
  def self.cast!(user, election, candidate, ballot)
    vote = new
    vote.election = election
    vote.candidate = candidate
    vote.uuid = UUID.random_create.to_s
    # TODO: detect UUID collision and generate new
    vote.save!
    ballot.cast!
    vote
  end
end
